package org.metaverse.restful;

import java.util.HashMap;
import java.util.Map;

import org.metaverse.warehouse.Admin;
import org.metaverse.warehouse.BulkUploader;
import org.metaverse.warehouse.ContextControl;
import org.metaverse.warehouse.Fetcher;
import org.metaverse.warehouse.Solver;
import org.metaverse.warehouse.Uploader;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;
import org.restlet.Application;
import org.restlet.Component;
import org.restlet.Restlet;
import org.restlet.Server;
import org.restlet.data.ChallengeScheme;
import org.restlet.data.Protocol;
import org.restlet.routing.Router;
import org.restlet.security.ChallengeAuthenticator;

public class Activator implements BundleActivator {
	
	private static BundleContext _context;
	private static Map<Class<?>, ServiceTracker> _trackers;

	private Component _component;

	
	static BundleContext getContext() {
		return _context;
	}
	
	@SuppressWarnings("unchecked")
	static final <T> T getService(Class<T> aSpi)
	{
		return (T)_trackers.get(aSpi).getService();
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
	 */
	public void start(final BundleContext bundleContext) throws Exception {
		_context = bundleContext;
		
		
		_trackers = initTrackers();
		
		
		
		_component = new Component();
		_component.setStatusService(new StatusServiceImpl(true));
		Server server = _component.getServers().add(Protocol.HTTP, 8182);
		server.getContext().getParameters().add("maxTotalConnections", "50");
		server.getContext().getParameters().add("maxThreads", "50");
		_component.getDefaultHost()
			.attach("/warehouse", new Application()
			{
				@Override
				public Restlet createInboundRoot() {
//					getMetadataService().addExtension("ttl", new MediaType("text/turtle"));
//					getMetadataService().addExtension("trig", new MediaType("text/trig"));
//					getMetadataService().addCommonExtensions();
//					System.out.println(getMetadataService().getAllMediaTypes("ttl"));
//					setStatusService(new StatusServiceImpl(true));
					Router router = new Router(getContext());
					router.attach("/dataset",DatasetResource.class); 
					// Create a Guard  
					System.out.println("createInboundRoot");
					ChallengeAuthenticator guard = new ChallengeAuthenticator(  
					                getContext(), ChallengeScheme.HTTP_BASIC, "Warehouse");  
					guard.setVerifier(new VerifierImpl(bundleContext));
					guard.setNext(router);
					
					return guard;
				}
			});
		_component.start();
	}

	
	@SuppressWarnings("rawtypes")
	private Map<Class<?>, ServiceTracker> initTrackers() {
		Class[] spis = {
				Fetcher.class,
				Uploader.class,
				BulkUploader.class,
				ContextControl.class,
				Admin.class,
				Solver.class
		};
		Map<Class<?>, ServiceTracker> trackers = new HashMap<Class<?>, ServiceTracker>();
		for(Class spi:spis)
		{
			ServiceTracker tracker = new ServiceTracker(_context, spi.getName(), null);
			trackers.put(spi, tracker);
			tracker.open();
		}
		
		return trackers;
	}

	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext bundleContext) throws Exception {
		_context = null;
		_component.stop();
		_component = null;
	}

}
